<template>
  <div class="operation-log-container">
    <div class="filter-container">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="操作模块">
          <el-input v-model="queryParams.module" placeholder="请输入操作模块" clearable />
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select v-model="queryParams.operationType" placeholder="请选择操作类型" clearable>
            <el-option label="创建" value="CREATE" />
            <el-option label="更新" value="UPDATE" />
            <el-option label="删除" value="DELETE" />
            <el-option label="查询" value="QUERY" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作结果">
          <el-select v-model="queryParams.status" placeholder="请选择操作结果" clearable>
            <el-option label="成功" :value="0" />
            <el-option label="失败" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="danger" @click="handleClean">清理日志</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="logList"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="username" label="用户名" width="120" />
      <el-table-column prop="module" label="操作模块" width="120" />
      <el-table-column prop="operationType" label="操作类型" width="100">
        <template #default="scope">
          <el-tag
            :type="getOperationTypeTag(scope.row.operationType)"
          >{{ scope.row.operationType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="操作描述" />
      <el-table-column prop="ip" label="IP地址" width="130" />
      <el-table-column prop="status" label="操作结果" width="100">
        <template #default="scope">
          <el-tag
            :type="scope.row.status === 0 ? 'success' : 'danger'"
          >{{ scope.row.status === 0 ? '成功' : '失败' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="操作时间" width="180" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDetail(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 日志详情对话框 -->
    <el-dialog
      title="日志详情"
      :visible.sync="dialogVisible"
      width="700px"
    >
      <div v-if="currentLog">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="用户名">{{ currentLog.username }}</el-descriptions-item>
          <el-descriptions-item label="操作模块">{{ currentLog.module }}</el-descriptions-item>
          <el-descriptions-item label="操作类型">{{ currentLog.operationType }}</el-descriptions-item>
          <el-descriptions-item label="操作描述">{{ currentLog.description }}</el-descriptions-item>
          <el-descriptions-item label="操作方法">{{ currentLog.method }}</el-descriptions-item>
          <el-descriptions-item label="IP地址">{{ currentLog.ip }}</el-descriptions-item>
          <el-descriptions-item label="操作结果">
            <el-tag :type="currentLog.status === 0 ? 'success' : 'danger'">
              {{ currentLog.status === 0 ? '成功' : '失败' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="操作时间">{{ currentLog.createTime }}</el-descriptions-item>
          <el-descriptions-item label="操作参数" :span="2">
            <pre>{{ currentLog.params }}</pre>
          </el-descriptions-item>
          <el-descriptions-item v-if="currentLog.errorMsg" label="错误信息" :span="2">
            <pre>{{ currentLog.errorMsg }}</pre>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 清理日志对话框 -->
    <el-dialog
      title="清理日志"
      :visible.sync="cleanDialogVisible"
      width="400px"
    >
      <el-form :model="cleanForm" label-width="100px">
        <el-form-item label="保留天数">
          <el-input-number v-model="cleanForm.days" :min="1" :max="365" />
          <div class="form-tip">将删除{{ cleanForm.days }}天之前的日志</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cleanDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmClean" :loading="cleanLoading">确认清理</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pageOperationLog, cleanOperationLog } from '@/api/operationLog'

export default {
  name: 'OperationLog',
  data() {
    return {
      loading: false,
      cleanLoading: false,
      logList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        module: '',
        operationType: '',
        status: null
      },
      dialogVisible: false,
      currentLog: null,
      cleanDialogVisible: false,
      cleanForm: {
        days: 30
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getOperationTypeTag(type) {
      const map = {
        'CREATE': 'success',
        'UPDATE': 'warning',
        'DELETE': 'danger',
        'QUERY': 'info'
      }
      return map[type] || ''
    },
    getList() {
      this.loading = true
      pageOperationLog(this.queryParams).then(response => {
        this.logList = response.data.records || []
        this.total = response.data.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        module: '',
        operationType: '',
        status: null
      }
      this.getList()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    handleDetail(row) {
      this.currentLog = row
      this.dialogVisible = true
    },
    handleClean() {
      this.cleanDialogVisible = true
    },
    confirmClean() {
      this.cleanLoading = true
      cleanOperationLog(this.cleanForm.days).then(response => {
        this.$message.success(`成功清理 ${response.data} 条日志记录`)
        this.cleanDialogVisible = false
        this.getList()
        this.cleanLoading = false
      }).catch(() => {
        this.cleanLoading = false
      })
    }
  }
}
</script>

<style scoped>
.operation-log-container {
  padding: 20px;
}
.filter-container {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}
pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  background-color: #f5f7fa;
  padding: 8px;
  margin: 0;
  border-radius: 4px;
  max-height: 200px;
  overflow-y: auto;
}
</style> 